package band.wukong.practice.mq.activemq.hello.topic;

import band.wukong.practice.mq.activemq.hello.HelloMessageListener;
import band.wukong.practice.mq.activemq.hello.MQServerConst;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author wukong(wukonggg@139.com)
 */
public class TopicSubscriber {

    private static final ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
            MQServerConst.ADMIN_NAME,
            MQServerConst.ADMIN_PASSWD,
            MQServerConst.BROKER_URL);

    public static void main(String[] args) throws JMSException {
        // Connection ：JMS 客户端到JMS Provider 的连接
        final Connection connection = connectionFactory.createConnection();

        connection.start();

        // Session： 一个发送或接收消息的线程
        final Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

        // Destination ：消息的目的地;消息送谁那获取.
        Destination destination = session.createTopic(MQServerConst.TOPIC_0); // 创建topic

        // 消费者，消息接收者
        MessageConsumer consumer1 = session.createConsumer(destination);
        consumer1.setMessageListener(new HelloMessageListener("C1", session));

        // 再来一个消费者，消息接收者
        MessageConsumer consumer2 = session.createConsumer(destination);
        consumer2.setMessageListener(new HelloMessageListener("C2", session));

    }
}
